package com.finance.cooperate.feature.reader;


import com.finance.cooperate.common.constant.Constant;
import com.finance.cooperate.dao.local.cache.SmsCategoryCache;
import com.finance.cooperate.feature.base.ReaderImpl;
import com.finance.cooperate.feature.core.feature.FeatureDefinition;
import com.finance.cooperate.feature.core.feature.smsphone.SmsOppositesV1;
import com.finance.cooperate.feature.core.feature.smswords.SmsWordsV1;
import com.finance.cooperate.feature.dto.CompositeSmsCountDTO;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @ClassName SmsReader
 * @Description 短信数据抽取
 * @Author shen
 * @Date 2022/5/26 22:47
 * @Modify ...
 */
public class SmsReader extends ReaderImpl {
    @Override
    public Map<String, String> read(String userId) {

        CompositeSmsCountDTO dto = smsService.getCompositeSmsCount(userId);

        // 判断是否存在短信
        super.put(FeatureDefinition.f_have_sms, dto.getHaveSms());

        super.put(FeatureDefinition.f_sms_fasttext_01_score, dto.getFastTextScore01());

        super.put(FeatureDefinition.f_sms_blistm_01_score, dto.getBlistm01());

        super.put(FeatureDefinition.f_sms_cnn_01_score, dto.getSmsScore01());


        // 基础信息统计
        Map<String, Object> basicInfoV1 = dto.getBasicInfoV1();
        if (basicInfoV1.size() > 0) {

            for (String k : SmsCategoryCache.getTimeSplitsV1().keySet()) {

                Integer receiveCount = (Integer) basicInfoV1.getOrDefault("receive_count_" + k, 0);
                super.put(FeatureDefinition.f_sms_basic_v1_.getValue() + "receive_count_" + k, receiveCount);

                Integer squareCount = (Integer) basicInfoV1.getOrDefault("square_count_" + k, 0);
                super.put(FeatureDefinition.f_sms_basic_v1_.getValue() + "square_count_" + k, squareCount);

                Integer phoneNumCount = (Integer) basicInfoV1.getOrDefault("phone_num_count_" + k, 0);
                super.put(FeatureDefinition.f_sms_basic_v1_.getValue() + "phone_num_count_" + k, phoneNumCount);

                Double phoneRate = (Double) basicInfoV1.getOrDefault("phone_rate_" + k, 0d);
                super.put(FeatureDefinition.f_sms_basic_v1_.getValue() + "phone_rate_" + k, phoneRate);

                Integer websiteCount = (Integer) basicInfoV1.getOrDefault("website_count_" + k, 0);
                super.put(FeatureDefinition.f_sms_basic_v1_.getValue() + "website_count_" + k, websiteCount);

                for (String speiType : Arrays.asList("receipt", "payment")) {
                    Integer count = (Integer) basicInfoV1.getOrDefault("spei_" + speiType + "_count_" + k, 0);
                    super.put(FeatureDefinition.f_sms_basic_v1_.getValue() + "spei_" + speiType + "_count_" + k, count);

                    Integer amount = (Integer) basicInfoV1.getOrDefault("spei_" + speiType + "_amount_" + k, 0);
                    super.put(FeatureDefinition.f_sms_basic_v1_.getValue() + "spei_" + speiType + "_amount_" + k, amount);

                }

                Integer incomeAmount = (Integer) basicInfoV1.getOrDefault("spei_income_amount_" + k, 0);
                super.put(FeatureDefinition.f_sms_basic_v1_.getValue() + "spei_income_amount_" + k, incomeAmount);


                for (Integer i : Arrays.asList(2, 6, 9, 12, 15, 20, 25, 30, 60)) {

                    Integer sameOppositeCount = (Integer) basicInfoV1.getOrDefault("same_opposite_" + k + "_" + i, 0);
                    super.put(FeatureDefinition.f_sms_basic_v1_.getValue() + "same_opposite_" + k + "_" + i, sameOppositeCount);

                }

                Integer sendCount = (Integer) basicInfoV1.getOrDefault("send_count_" + k, 0);
                super.put(FeatureDefinition.f_sms_basic_v1_.getValue() + "send_count_" + k, sendCount);



                Integer receiveDaysCount = (Integer) basicInfoV1.getOrDefault("receive_days_count_" + k, 0);
                super.put(FeatureDefinition.f_sms_basic_v1_.getValue() + "receive_days_count_" + k, receiveDaysCount);


                Integer sendDaysCount = (Integer) basicInfoV1.getOrDefault("send_days_count_" + k, 0);
                super.put(FeatureDefinition.f_sms_basic_v1_.getValue() + "send_days_count_" + k, sendDaysCount);


            }




            for (Integer t : Arrays.asList(30, 15, 7, 3, 1, 0, -1, -3, -7, -15, -30, Integer.MIN_VALUE)) {

                if (t.equals(Integer.MIN_VALUE)) {
                    Integer count = (Integer) basicInfoV1.getOrDefault("contain_date_diff_d", 0);
                    super.put(FeatureDefinition.f_sms_basic_v1_.getValue() + "contain_date_diff_d", count);
                } else if (t >= 0) {
                    Integer count = (Integer) basicInfoV1.getOrDefault("contain_date_diff_" + t, 0);
                    super.put(FeatureDefinition.f_sms_basic_v1_.getValue() + "contain_date_diff_" + t, count);
                } else {
                    Integer count = (Integer) basicInfoV1.getOrDefault("contain_date_diff_d" + Math.abs(t), 0);
                    super.put(FeatureDefinition.f_sms_basic_v1_.getValue() + "contain_date_diff_d" + Math.abs(t), count);
                }

            }



        }


        // 关键词短信注入
        Map<String, Integer> words01 = dto.getWords01();
        SmsWordsV1.words.forEach(w -> {
            super.put(FeatureDefinition.f_sms_words_.getValue() + w, words01.getOrDefault(w, 0));
        });


        // 常见发送方统计
        Map<String, Long> oppositesV1 = dto.getOppositesV1();

        for (String opp : SmsOppositesV1.phone) {

            opp = opp.replace(" ", "_");

            String finalOpp = opp;
            SmsCategoryCache.getTimeSplitsV1().forEach((k, v) -> {

                for (Integer i : Arrays.asList(0, 1, 2, 3, 4, 5)) {

                    Long isHit = oppositesV1.getOrDefault("send_" + finalOpp + "_" + k + "_" + i, Constant.IS_NO_HIT);

                    super.put(FeatureDefinition.f_sms_oppo_.getValue() + finalOpp + "_" + k + "_" + i, isHit);

                }
            });
        }


        Map<String, Integer> loanInfoV1 = dto.getLoanInfoV1();
        SmsCategoryCache.getTimeSplitsV1().forEach((k, v) -> {

            Integer overdue = loanInfoV1.getOrDefault("loan_overdue_" + k, 0);
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "overdue_" + k, overdue);

            Integer amountOverdueMax = loanInfoV1.getOrDefault("loan_overdue_amount_max_" + k, Constant.MISSING_VALUE.intValue());
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "overdue_amount_max_" + k, amountOverdueMax);

            Integer amountOverdueMin = loanInfoV1.getOrDefault("loan_overdue_amount_min_" + k, Constant.MISSING_VALUE.intValue());
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "overdue_amount_min_" + k, amountOverdueMin);

            Integer amountOverdueAll = loanInfoV1.getOrDefault("loan_overdue_amount_all_" + k, Constant.MISSING_VALUE.intValue());
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "overdue_amount_sum_" + k, amountOverdueAll);

            Integer amountOverdueCount = loanInfoV1.getOrDefault("loan_overdue_amount_count_" + k, Constant.MISSING_VALUE.intValue());
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "overdue_amount_count_" + k, amountOverdueCount);

            Integer amountOverdueAvg = loanInfoV1.getOrDefault("loan_overdue_amount_avg_" + k, Constant.MISSING_VALUE.intValue());
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "overdue_amount_avg_" + k, amountOverdueAvg);



            Integer loan = loanInfoV1.getOrDefault("loan_loan_" + k, 0);
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "loan_" + k, loan);

            Integer amountLoanMax = loanInfoV1.getOrDefault("loan_loan_amount_max_" + k, Constant.MISSING_VALUE.intValue());
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "loan_amount_max_" + k, amountLoanMax);

            Integer amountLoanMin = loanInfoV1.getOrDefault("loan_loan_amount_min_" + k, Constant.MISSING_VALUE.intValue());
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "loan_amount_min_" + k, amountLoanMin);

            Integer amountLoanAll = loanInfoV1.getOrDefault("loan_loan_amount_all_" + k, Constant.MISSING_VALUE.intValue());
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "loan_amount_sum_" + k, amountLoanAll);

            Integer amountLoanCount = loanInfoV1.getOrDefault("loan_loan_amount_count_" + k, Constant.MISSING_VALUE.intValue());
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "loan_amount_count_" + k, amountLoanCount);

            Integer amountLoanAvg = loanInfoV1.getOrDefault("loan_loan_amount_avg_" + k, Constant.MISSING_VALUE.intValue());
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "loan_amount_avg_" + k, amountLoanAvg);


            Integer pay = loanInfoV1.getOrDefault("loan_pay_" + k, 0);
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "pay_" + k, pay);

            Integer income = loanInfoV1.getOrDefault("loan_income_" + k, 0);
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "income_" + k, income);



            Integer common = loanInfoV1.getOrDefault("loan_common_" + k, 0);
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "common_" + k, common);



            Integer overdueCompetition = loanInfoV1.getOrDefault("loan_overdue_top_competition_" + k, 0);
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "overdue_top_competition_" + k, overdueCompetition);

            Integer loanCompetition = loanInfoV1.getOrDefault("loan_loan_top_competition_" + k, 0);
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "loan_top_competition_" + k, loanCompetition);


            Integer overdueTala = loanInfoV1.getOrDefault("loan_overdue_tala_" + k, 0);
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "overdue_tala_" + k, overdueTala);

            Integer loanTala = loanInfoV1.getOrDefault("loan_loan_tala_" + k, 0);
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "loan_tala_" + k, loanTala);


            Integer overdueStori = loanInfoV1.getOrDefault("loan_overdue_stori_" + k, 0);
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "overdue_stori_" + k, overdueStori);

            Integer loanStori = loanInfoV1.getOrDefault("loan_loan_stori_" + k, 0);
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "loan_stori_" + k, loanStori);



            Integer overdueSuperapoyo = loanInfoV1.getOrDefault("loan_overdue_superapoyo_" + k, 0);
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "overdue_superapoyo_" + k, overdueSuperapoyo);

            Integer loanSuperapoyo = loanInfoV1.getOrDefault("loan_loan_superapoyo_" + k, 0);
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "loan_superapoyo_" + k, loanSuperapoyo);


            Integer diasMax = loanInfoV1.getOrDefault("loan_dias_max_" + k, Constant.MISSING_VALUE.intValue());
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "dias_max_" + k, diasMax);

            Integer diasMin = loanInfoV1.getOrDefault("loan_dias_min_" + k, Constant.MISSING_VALUE.intValue());
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "dias_min_" + k, diasMin);



            Integer diasCount = loanInfoV1.getOrDefault("loan_dias_count_" + k, Constant.MISSING_VALUE.intValue());
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "dias_count_" + k, diasCount);

            Integer diasAvg = loanInfoV1.getOrDefault("loan_dias_avg_" + k, Constant.MISSING_VALUE.intValue());
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "dias_avg_" + k, diasAvg);

            Integer diasMedian = loanInfoV1.getOrDefault("loan_dias_median_" + k, Constant.MISSING_VALUE.intValue());
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "dias_median_" + k, diasMedian);


            Integer diasDistinctCount = loanInfoV1.getOrDefault("loan_dias_distinct_count_" + k, Constant.MISSING_VALUE.intValue());
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "dias_distinct_count_" + k, diasDistinctCount);

            Integer diasDistinctAvg = loanInfoV1.getOrDefault("loan_dias_distinct_avg_" + k, Constant.MISSING_VALUE.intValue());
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "dias_distinct_avg_" + k, diasDistinctAvg);

            Integer diasDistinctMedian = loanInfoV1.getOrDefault("loan_dias_distinct_median_" + k, Constant.MISSING_VALUE.intValue());
            super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "dias_distinct_median_" + k, diasDistinctMedian);


        });

        Integer overdueOpposite1w = loanInfoV1.getOrDefault("loan_overdue_opposite_1w", 0);
        super.put(FeatureDefinition.f_sms_loan_v1_.getValue() + "overdue_opposite_1w", overdueOpposite1w);



        return super.getResult();
    }
}
